package cn.iocoder.yudao.module.worker.controller.admin.reward;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.worker.controller.admin.reward.vo.*;
import cn.iocoder.yudao.module.worker.dal.dataobject.reward.WorkerRewardDO;
import cn.iocoder.yudao.module.worker.service.reward.WorkerRewardService;

@Tag(name = "管理后台 - 打赏记录")
@RestController
@RequestMapping("/worker/reward")
@Validated
public class WorkerRewardController {

    @Resource
    private WorkerRewardService rewardService;

    @PostMapping("/create")
    @Operation(summary = "创建打赏记录")
    @PreAuthorize("@ss.hasPermission('worker:reward:create')")
    public CommonResult<Long> createReward(@Valid @RequestBody WorkerRewardSaveReqVO createReqVO) {
        return success(rewardService.createReward(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新打赏记录")
    @PreAuthorize("@ss.hasPermission('worker:reward:update')")
    public CommonResult<Boolean> updateReward(@Valid @RequestBody WorkerRewardSaveReqVO updateReqVO) {
        rewardService.updateReward(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除打赏记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('worker:reward:delete')")
    public CommonResult<Boolean> deleteReward(@RequestParam("id") Long id) {
        rewardService.deleteReward(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得打赏记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('worker:reward:query')")
    public CommonResult<WorkerRewardRespVO> getReward(@RequestParam("id") Long id) {
        WorkerRewardDO reward = rewardService.getReward(id);
        return success(BeanUtils.toBean(reward, WorkerRewardRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得打赏记录分页")
    @PreAuthorize("@ss.hasPermission('worker:reward:query')")
    public CommonResult<PageResult<WorkerRewardRespVO>> getRewardPage(@Valid WorkerRewardPageReqVO pageReqVO) {
        PageResult<WorkerRewardDO> pageResult = rewardService.getRewardPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WorkerRewardRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出打赏记录 Excel")
    @PreAuthorize("@ss.hasPermission('worker:reward:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRewardExcel(@Valid WorkerRewardPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WorkerRewardDO> list = rewardService.getRewardPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "打赏记录.xls", "数据", WorkerRewardRespVO.class,
                        BeanUtils.toBean(list, WorkerRewardRespVO.class));
    }

}